const path = require("path"); //引入path模块
// 引入jq需要加入以下代码
const webpack = require("webpack");
const {
  qiniuConfig: { prefix },
} = require('./deploy/config');

function resolve(dir) {
  return path.join(__dirname, dir); //path.join(__dirname)设置绝对路径
}

module.exports = {
  publicPath: process.env.NODE_ENV === "pro" ? prefix : "/",
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.plugins.delete("preload"); // TODO: need test
    config.plugins.delete("prefetch"); // TODO: need test
    config.resolve.alias.set("@", resolve("./src"));

    config.when(process.env.NODE_ENV !== "development", (config) => {
      config
      .plugin('ScriptExtHtmlWebpackPlugin')
      .after('html')
      .use('script-ext-html-webpack-plugin', [
          {
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
          }
      ])
      .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial", // only package third parties that are initially dependent
          },
          elementUI: {
            name: "chunk-elementUI", // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
          },
          vuedraggable: {
            name: "chunk-vuedraggable", // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?vuedraggable(.*)/, // in order to adapt to cnpm
          },
          highlightJs: {
            name: "chunk-highlightJs", // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?highlight.js(.*)/, // in order to adapt to cnpm
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      });
      config.optimization.runtimeChunk("single");
    });

    config.when(process.env.NODE_ENV === "production", (config) => {
      const cdn = {
        js: [
          "https://cdn.staticfile.org/vue/2.6.11/vue.min.js",
          "https://cdn.staticfile.org/vue-router/3.1.3/vue-router.min.js",
          "https://cdn.staticfile.org/axios/0.18.0/axios.min.js",
          "https://cdn.staticfile.org/echarts/4.1.0/echarts.min.js",
        ],
        css: [],
      };
      config.set("externals", {
        vue: "Vue",
        "vue-router": "VueRouter",
        axios: "axios",
        echarts: "echarts",
      });
      config.plugin("html").tap((args) => {
        args[0].isProd = true;
        args[0].cdn = cdn;
        return args;
      });
    });
  },
};
